package com.powergisol.gis.geom;


import com.powergisol.core.math.Dot;
import com.powergisol.core.math.Line;
import com.powergisol.exception.GeoBuildException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.io.WKTWriter;

import java.util.Arrays;
import java.util.List;

public class GeoLineString extends GeoType {

    private Coordinate[] coordinates;

    public GeoLineString(double x1,double y1,double x2,double y2){
        this(x1,y1,0,x2,y2,0);
    }


    public GeoLineString(double x1,double y1,double z1,double x2,double y2,double z2){

        Coordinate coordinate1 = new Coordinate(x1,y1,z1);
        Coordinate coordinate2 = new Coordinate(x2,y2,z2);
        coordinates = new Coordinate[]{coordinate1,coordinate2};

    }


    public GeoLineString(Coordinate coordinateA,Coordinate coordinateB){
        coordinates = new Coordinate[]{coordinateA,coordinateB};
    }

    public Coordinate[] getCoordinates() {
        return coordinates;
    }

    public GeoLineString(double[][] xyz){

        if(xyz.length!=2){
            throw new GeoBuildException("直线必须且只能包含两个点");
        }

        double[] coord1 =  xyz[0];
        double[] coord2 =  xyz[1];

        if(coord1.length!=coord2.length || coord2.length!=3){
            throw new GeoBuildException("坐标点必须包含X,Y,Z三个数值!");
        }

        Coordinate coordinate1 = new Coordinate(coord1[0],coord1[1],coord1[2]);
        Coordinate coordinate2 = new Coordinate(coord2[0],coord2[1],coord2[2]);
        coordinates = new Coordinate[]{coordinate1,coordinate2};
    }


    public GeoLineString(List<double[]> xyz){

        this(xyz.toArray(new double[xyz.size()][]));
    }


    @Override
    public LineString toGeometry() {
        return geometryFactory.createLineString(this.coordinates);
    }

    @Override
    public String toWKT() {
        return WKTWriter.toLineString(this.coordinates);
    }

    public Line toLine(){

       Dot dotA = new Dot(coordinates[0].x,coordinates[0].y,coordinates[0].z);
       Dot dotB = new Dot(coordinates[1].x,coordinates[1].y,coordinates[1].z);

       return new Line(dotA,dotB,true);
    }
}
